package com.study.vertx;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Yuqingsong
 */
public class HttpServer extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        AtomicInteger counter = new AtomicInteger(0);
        vertx.createHttpServer().requestHandler(req -> {
            int i = counter.incrementAndGet();
            System.out.println(req.absoluteURI()+"\t"+i+"\t header:"+req.getHeader("classid"));
            req.response().putHeader("content-type", "text/html;charset=utf-8")
                    .end("<html><body><h1>你好 vert.x!"+counter.get()+"</h1></body></html>");
//            vertx.setTimer(3000,h->{
//                int i = counter.incrementAndGet();
//                System.out.println(req.absoluteURI()+"\t"+i+"\t header:"+req.getHeader("classid"));
//                req.response().putHeader("content-type", "text/html;charset=utf-8")
//                        .end("<html><body><h1>你好 vert.x!"+counter.get()+"</h1></body></html>");
//            });
        }).listen(10086);

        vertx.createHttpServer().requestHandler(req -> {
//                System.out.println(req.absoluteURI()+"\t header:"+req.getHeader("classid"));
                counter.incrementAndGet();
                req.response().putHeader("content-type", "text/html;charset=utf-8")
                        .end("<html><body><h1>你好 vert.x!"+counter.get()+"</h1></body></html>");
        }).listen(2048);
    }

    public static void main(String[] args) {
        Vertx.vertx().deployVerticle(HttpServer.class.getName());
    }
}